﻿using Anke.SHManage.BLL;
using BLL;
using Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Utility;

namespace SHTYWeb.Controllers
{
    public class HomeController : Controller
    {
        /// <summary>
        /// 用户登录页
        /// </summary>
        /// <returns></returns>
        [SkipLoginAttribute]
        public ActionResult UserLoginPage()
        {
            return View();
        }

        public ActionResult Index()
        {
            ViewBag.UserID = UserOperateContext.Current.Session_UsrInfo.BianMa; ;//用户编码
            ViewBag.UserWorkID = UserOperateContext.Current.Session_UsrInfo.GongHao;    //工号
            ViewBag.UserName = UserOperateContext.Current.Session_UsrInfo.XingMing;    //姓名
            ViewBag.DengLuMing = UserOperateContext.Current.Session_UsrInfo.DengLuMing;    //登录名
            return View();
        }

        /// <summary>
        /// Filters跳转到登录页面
        /// </summary>
        /// <returns></returns>
        [SkipLoginAttribute]
        public ActionResult RedirectToLogin()
        {
            return View();
        }

        /// <summary>
        /// 系统首页
        /// </summary>
        /// <returns></returns>
        public ActionResult FirstPage()
        {
            ViewBag.UserID = UserOperateContext.Current.Session_UsrInfo.BianMa;//用户编码
            return View();
        }


        /// <summary>
        /// 提交登录请求
        /// </summary>
        /// <param name="info">登录实体</param>
        /// <returns></returns>
        [HttpPost]
        [SkipLoginAttribute]
        public ActionResult UserLoginByInfo(LoginInfo info)
        {
            //首先检查用户名密码
            UserLoginBLL bll = new UserLoginBLL();
            if (bll.Login(info.LoginName, info.PassWord))
            {
                //StateLog("登录", info.YongHuMing); //记录用户登录状态---暂时搁置
                //登录成功，跳转到主页面
                JsonResult jsr = this.JsonResult(E_JsonResult.OK, "", null, "~/Home/Index");
                jsr.ContentType = "text/html";
                return jsr;
            }
            else
            {
                //登录失败,发送错误信息给用户
                JsonResult jsr = this.JsonResult(E_JsonResult.Error, "登录失败！用户名或者密码错误！", null, null);
                jsr.ContentType = "text/html";
                return jsr;
            }
        }

        /// <summary>
        /// 退出登录
        /// </summary>
        /// <returns></returns>
        public ActionResult ExitLogin(string YongHuMing)
        {
            //int num = StateLog("退出登录", YongHuMing); //2019-10-22 plq 记录退出登录状态日志,成功返回1
            //if (num > 0) //若记录状态成功
            //{
            //    //清除session---防止原链接仍可进入首页
            //    //Session.Abandon();//清除所有session
            //    Session.Remove("UserInfo"); //只清除用户信息session相关
            //    return Json("OK");
            //}
            //else
            //{
            //    return Json("NO");
            //}
            try
            {
                Session.Remove("UserInfo"); //清除用户信息session
                return Json("OK");
            }
            catch (Exception ex)
            {
                LogUtility.Error("Home/ExitLogin", ex.Message);
                return Json("NO");
            }
        }


    }
}